package BD;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.example.zengyan.myphonebook.LoginActivity;
import com.example.zengyan.myphonebook.MainActivity;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.litepal.crud.DataSupport;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import Bean.Member;
import Bean.Organization;
import okhttp3.Call;
import static BD.mConfig.*;
/**
 * Created by zengyan on 2016/6/15.
 */
public class HttpUtil {


    public static void login(final Context context, final String stnnum, String password,final Callback callback) {
//        String url="http://wechat.gcu.ikk.me/tmp_data_socket.php?token=9CFF6CEAF3C4E35CFE92DD23E85F1&xh=201430098034&mm=zengyan060039";
        String url="http://wechat.gcu.ikk.me/tmp_data_socket.php";
        OkHttpUtils
                .get()
                .url(url)
                .addParams("token","9CFF6CEAF3C4E35CFE92DD23E85F1")
                .addParams("xh",stnnum)
                .addParams("mm",password)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }
                    @Override
                    public void onResponse(String response, int id) {

                        if (response.equals("true")) {

                            getMyAllOrganzition(stnnum,callback);
                        }else {
                            Toast.makeText(context, "学号或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
    public static void getMyAllOrganzition(String stunum, final Callback callback) {
        String url = "http://172.31.113.51:8080/SS2HDome/android!write.action";
//        String url = "http://192.168.137.1:8080/SS2HDome/android!write.action";
        ArrayList<Organization> allO = new ArrayList<Organization>();
        OkHttpUtils
                .get()
                .url(url)
                .addParams("Mstunum", stunum)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callback.onFail();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson g = new Gson();
                        Type type = new TypeToken<List<Organization>>() {
                        }.getType();
                        List<Organization> list = g.fromJson(response, type);
                        DataSupport.deleteAll(Organization.class);
                        DataSupport.deleteAll(Bean.Member.class);
                        for (Organization o : list) {
                            Log.d(TAG, "onResponse: "+o.getOid());
                            o.save();
                            for (Bean.Member member:o.getMembers() ) {
                                member.setOrganization(o);
                                member.save();
                            }
                        }

                        callback.onSuccess(response);
                    }
                });
    }

    public static interface Callback{
        void onSuccess(String result);
        void onFail();
    }
}